package com.toman.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.toman.R;


/**
 * @Description: TabHost Activity
 * @Author: 邹苏启
 * @Since: 2016/11/28 下午9:04
 */
public abstract class TabHostActivity extends FragmentActivity {

    protected FragmentTabHost mTabHost;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_tabhost);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(getApplicationContext(), getSupportFragmentManager(), R.id.realtabcontent);

        Class<?>[] tabFrag = getTabFragment();
        for (int i = 0, len = tabFrag.length; i < len; i++) {
            mTabHost.addTab(mTabHost.newTabSpec(getString(getTabwidgetTag()[i])).setIndicator(getTabItemView(i)), tabFrag[i], bundle);
//            mTabHost.setOnTabChangedListener();
        }

        mTabHost.getTabWidget().getChildTabViewAt(2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    v.findViewById(R.id.iv_red).setVisibility(View.GONE);
                    mTabHost.setCurrentTab(2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


        try {
            mTabHost.setCurrentTab(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 与tabwidget相对应的 碎片 数组
     *
     * @return
     */
    protected abstract Class<?>[] getTabFragment();

    /**
     * 与tabwidget相对应的 图片 数组
     *
     * @return
     */
    protected abstract int[] getTabwidgetDrawable();

    /**
     * 与tabwidget相对应的 Tag 数组
     *
     * @return
     */
    protected abstract int[] getTabwidgetTag();


    private View getTabItemView(int index) {
        View tabView = View.inflate(this, R.layout.tab_item_view, null);
        ImageView imgView = (ImageView) tabView.findViewById(R.id.tab_img);
        imgView.setBackgroundResource(getTabwidgetDrawable()[index]);
        TextView textView = (TextView) tabView.findViewById(R.id.tab_text);
        textView.setText(getTabwidgetTag()[index]);
        return tabView;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mTabHost = null;
    }




}
